import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;

public class MainScreen {

	// Create a new JFrame with a title of "Coverage Corner"
	JFrame newFrame = new JFrame("Coverage Corner - Main Screen");
	RecentScore[] recentScores;
	MatchUp[] matchUPS;
	League league;
	Team TeamA;
	Team TeamB;
	int teamAScore;
	int teamBScore;
	RecentScoresScreen rSS = new RecentScoresScreen();
	LiveVideoScreen LVS = new LiveVideoScreen();
	MatchUpScreen mUS = new MatchUpScreen();
	LeagueScreen LS = new LeagueScreen();
	
	public MainScreen() {
		
	}
	
	public void runMainScreen(League L, MatchUp[] matchup, RecentScore[] recent) {
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		int screenWidth = ((int) tk.getScreenSize().getWidth());
		int screenHeight = ((int) tk.getScreenSize().getHeight());
		
		this.league = L;
		this.matchUPS = matchup;
		this.recentScores = recent;
		

		// Set background color to black
		Container con = newFrame.getContentPane();
		con.setBackground(Color.BLACK);
		// Make sure sizing is alright
		newFrame.validate();
		
		
		//Create new panel, -> set the background to red
		JPanel topFive = new JPanel();
		topFive.setBackground(Color.red);
		//Create a new vertical box layout, that way when we add the top five, it will go top to bottom
		topFive.setLayout(new BoxLayout(topFive, BoxLayout.Y_AXIS));
		//Set the box to the top-left corner of the screen, then top and then left
		topFive.setBounds(0,0,screenWidth/2,screenHeight/2);
		topFive.setAlignmentX(Component.LEFT_ALIGNMENT);
		topFive.setAlignmentY(Component.TOP_ALIGNMENT);
		
		String rankData[][] = new String[6][5];
		rankData[0][0] = "";
		rankData[0][0] = "    Rank";
		rankData[0][1] = "";
		rankData[0][2] = "Total Points";
		rankData[0][3] = "Streak";
		rankData[0][4] = "Moves";

		/*// This is where we start adding the JLabels to each of the panels. First thing is the rank,score,streak, and moves default text
		JLabel teamMemberInfo = new JLabel("    Rank                                            Total Points        Streak        Moves");
		//This sets the font to a bigger size, since it's fairly small to begin with.
		Font newF = new Font(teamMemberInfo.getFont().getName(),teamMemberInfo.getFont().getStyle(),(int)topFive.getWidth()/2/20);
		// This is what actually sets the text for the JLabel
		teamMemberInfo.setFont(newF);
		// Finally we add the JLabel to the  JPanel.
		topFive.add(teamMemberInfo);
		*/
		
//		JLabel blk = new JLabel("\n");
//		topFive.add(blk);
		// This loop is going to get the top 5 teams and output their information to the screen.
		for (int k=1; k <= 5; k++) {
			Team newTeam = L.GetMember(k);
	
			rankData[k][0] = "        " + Integer.toString(k);
			rankData[k][1] = newTeam.GetTeamName() + " (" + newTeam.GetRecord()+ ")";
			rankData[k][2] = Integer.toString(newTeam.CalculateTotalPoints());
			rankData[k][3] = newTeam.GetStreak();
			rankData[k][4] = Integer.toString(newTeam.CalculateTotalMoves());

			/*//Output the information in String format, -> change the font sizes of both the blank label and the info label
			JLabel i = new JLabel("       " + (k) + "        " + newTeam.GetTeamName() + "  (" + newTeam.GetRecord()+")                     " +
				+ newTeam.CalculateTotalPoints() + "                    " + newTeam.GetStreak() + "        " + newTeam.CalculateTotalMoves());
		
			// There is a bunch of spaces at the end right above this comment
			Font newI = new Font(i.getFont().getName(),i.getFont().getStyle(),(int)topFive.getWidth()/2/20);
			i.setFont(newI);
			

			//Add the labels to the panel
			topFive.add(i);
		
			
			//Create a blank JLabel that contains a newline character to space out the team infos
			JLabel blank = new JLabel("\n");
			Font newB = new Font(blank.getFont().getName(),blank.getFont().getStyle(),(int)topFive.getWidth()/2/15);
			blank.setFont(newB);
			topFive.add(blank);*/
			
		}
		
		String colarr[] = new String[5];
		DefaultTableModel mol= new DefaultTableModel(rankData,colarr);
		JTable rankTable = new JTable(mol);
		rankTable.setBackground(Color.RED);
		rankTable.setForeground(Color.BLACK);
		rankTable.setRowHeight(60);
		rankTable.getColumnModel().getColumn(0).setPreferredWidth(10);
		rankTable.getColumnModel().getColumn(1).setPreferredWidth(180);
		rankTable.setShowGrid(false);
		Font newF = new Font("rankTable", Font.BOLD,(int)topFive.getWidth()/40);
		rankTable.setFont(newF);
		
		topFive.add(rankTable);
		
		
		MouseListener topFiveListener = new MouseAdapter() {
			  public void mouseClicked(MouseEvent event) {
			    newFrame.setVisible(false); // figure out the source
			    LS.runLeagueScreen(league, matchUPS,recentScores); // display the text in the clicked label
			    // message is a JLabel
			  }
			};
		topFive.addMouseListener(topFiveListener);
		rankTable.addMouseListener(topFiveListener);
		
		
		//Create new panel, -> set the background to black
		JPanel recentUpdates = new JPanel();
		recentUpdates.setBackground(Color.blue);
		//Create a new vertical box layout, that way when we add the recent updates, it will go top to bottom
		recentUpdates.setLayout(new BoxLayout(recentUpdates, BoxLayout.Y_AXIS));
		//Set the box to the top-left corner of the screen, then top and then left.
		recentUpdates.setBounds(0,screenHeight - screenHeight/2,((int)screenWidth/2),((int)screenHeight/2));
		recentUpdates.setAlignmentX(Component.LEFT_ALIGNMENT);
		recentUpdates.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		
		String recentData[][] = new String[7][3];
		// This for loop is going to get the 6 most recent updated scores and add them to the recent scores Panel
		for (int x = 0; x < 6; x++) {
			RecentScore rS = recent[x];
			
			recentData[x][0] = "        +" + rS.GetPlusPoints();
			recentData[x][1] = rS.GetTeamName();
			recentData[x][2] = rS.GetDescription();
			/*//Create a blank JLabel that contains a newline character to space out the recent scores
			JLabel blank = new JLabel("\n");
			JLabel i = new JLabel("      +" + rS.GetPlusPoints() + "        " + rS.GetTeamName() + "                            " + rS.GetDescription());
			//Change the font sizes of both the recent scores info and the blank label
			Font newI = new Font(i.getFont().getName(),i.getFont().getStyle(),(int)recentUpdates.getWidth()/2/20);
			Font newB = new Font(blank.getFont().getName(),blank.getFont().getStyle(),(int)recentUpdates.getWidth()/2/15);
			i.setFont(newI);
			// Change the text color
			i.setForeground(Color.white);
			blank.setFont(newB);
			// Add the 2 labels to the recentUpdates Panel
			recentUpdates.add(blank);
			recentUpdates.add(i);*/
			
		}
		String colArr[] = new String[3];
		DefaultTableModel model= new DefaultTableModel(recentData,colArr);
		JTable recentTable = new JTable(model);
		recentTable.setBackground(Color.BLUE);
		recentTable.setForeground(Color.WHITE);
		recentTable.setRowHeight(60);
		recentTable.getColumnModel().getColumn(0).setPreferredWidth(20);
		recentTable.getColumnModel().getColumn(1).setPreferredWidth(80);
		recentTable.getColumnModel().getColumn(2).setPreferredWidth(400);
		recentTable.setShowGrid(false);
		recentTable.setFont(newF);
		
		recentUpdates.add(recentTable);
		
		MouseListener recentListener = new MouseAdapter() {
			  public void mouseClicked(MouseEvent event) {
			    newFrame.setVisible(false); // figure out the source
			    rSS.runRecentScoresScreen(league, matchUPS,recentScores); // display the text in the clicked label
			    // message is a JLabel
			  }
			};
		recentUpdates.addMouseListener(recentListener);
		recentTable.addMouseListener(recentListener);
		
		
		//Create new panel, -> set the background to green
		JPanel matchups = new JPanel();
		matchups.setBackground(Color.green);
		//Since it is going to be some matchups, going to have a grid with buttons where the labels on the buttons are the matchups.
		matchups.setLayout(new GridLayout(3,2));
		//This is so the matchups go from left to right
		matchups.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		//Sets the correct bounds
		matchups.setBounds(screenWidth - screenWidth/2, screenHeight - screenHeight/2, screenWidth/2, screenHeight/2);
		matchups.setAlignmentX(Component.RIGHT_ALIGNMENT);
		matchups.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		
		for (int o = 0; o < matchup.length; o++) {
			//Get the matchup
			MatchUp match = matchup[o];
			// Get the teams -> get team names, -> get team scores
			this.TeamA = match.getTeam('A');
			this.TeamB = match.getTeam('B');
			this.teamAScore = match.getTeamScore('A');
			this.teamBScore = match.getTeamScore('B');
			
			String matchupData[][] = new String[2][2];
			matchupData[0][0] = TeamA.GetTeamName();
			matchupData[0][1] = Integer.toString(match.getTeamScore('A'));
			matchupData[1][0] = TeamB.GetTeamName();
			matchupData[1][1] = Integer.toString(match.getTeamScore('B'));
			
			String colTemp[] = new String[2];
			DefaultTableModel modelTemp= new DefaultTableModel(matchupData,colTemp);
			JTable matchupTable = new JTable(modelTemp);
			matchupTable.setBackground(Color.lightGray);
			matchupTable.setForeground(Color.BLACK);
			matchupTable.setRowHeight(40);
			matchupTable.getColumnModel().getColumn(0).setPreferredWidth(150);
			matchupTable.setShowGrid(false);
			matchupTable.setFont(newF);
			
			JButton matchupButton = new JButton();
			matchupButton.setBackground(Color.lightGray);
			
			/*String teamAName = TeamA.GetTeamName();
			String teamBName = TeamB.GetTeamName();
			//Create the labels for the button to be output.
			JLabel teamALabel = new JLabel("  " + teamAName + "        " + teamAScore);
			JLabel teamBLabel = new JLabel("  " + teamBName + "        " + teamBScore);
			//Create the button, -> set the box layout so that everytime something is added, it goes right beneath it.
			
			matchupButton.setLayout(new BoxLayout(matchupButton,BoxLayout.Y_AXIS));
			//Update the fonts for the Labels.
			Font newTAL = new Font(teamALabel.getFont().getName(),teamALabel.getFont().getStyle(),(int)matchups.getWidth()/2/20);
			Font newTBL = new Font(teamALabel.getFont().getName(),teamALabel.getFont().getStyle(),(int)matchups.getWidth()/2/20);
			//Set the fonts for the two labels.
			teamALabel.setFont(newTAL);
			teamBLabel.setFont(newTBL);*/
			
			MouseListener MatchUpListener1 = new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					newFrame.setVisible(false); // figure out the source
					mUS.runMatchUpScreen(league, matchUPS, recentScores, matchUPS[0].getTeam('A'),
							matchUPS[0].getTeam('B'), matchUPS[0].getTeamScore('A'), matchUPS[0].getTeamScore('B')); // display the text in the clicked label
					// message is a JLabel
				}
			};
			MouseListener MatchUpListener2 = new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					newFrame.setVisible(false); // figure out the source
					mUS.runMatchUpScreen(league, matchUPS, recentScores, matchUPS[1].getTeam('A'),
							matchUPS[1].getTeam('B'), matchUPS[1].getTeamScore('A'), matchUPS[1].getTeamScore('B')); // display the text in the clicked label
					// message is a JLabel
				}
			};
			MouseListener MatchUpListener3 = new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					newFrame.setVisible(false); // figure out the source
					mUS.runMatchUpScreen(league, matchUPS, recentScores, matchUPS[2].getTeam('A'),
							matchUPS[2].getTeam('B'), matchUPS[2].getTeamScore('A'), matchUPS[2].getTeamScore('B')); // display the text in the clicked label
					// message is a JLabel
				}
			};
			MouseListener MatchUpListener4 = new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					newFrame.setVisible(false); // figure out the source
					mUS.runMatchUpScreen(league, matchUPS, recentScores, matchUPS[3].getTeam('A'),
							matchUPS[3].getTeam('B'), matchUPS[3].getTeamScore('A'), matchUPS[3].getTeamScore('B')); // display the text in the clicked label
					// message is a JLabel
				}
			};
			MouseListener MatchUpListener5 = new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					newFrame.setVisible(false); // figure out the source
					mUS.runMatchUpScreen(league, matchUPS, recentScores, matchUPS[4].getTeam('A'),
							matchUPS[4].getTeam('B'), matchUPS[4].getTeamScore('A'), matchUPS[4].getTeamScore('B')); // display the text in the clicked label
					// message is a JLabel
				}
			};
			
			if (o == 0) {
				matchupButton.addMouseListener(MatchUpListener1);
				matchupTable.addMouseListener(MatchUpListener1);
			} else if (o == 1) {
				matchupButton.addMouseListener(MatchUpListener2);
				matchupTable.addMouseListener(MatchUpListener2);
			} else if (o == 2) {
				matchupButton.addMouseListener(MatchUpListener3);
				matchupTable.addMouseListener(MatchUpListener3);
			} else if (o == 3) {
				matchupButton.addMouseListener(MatchUpListener4);
				matchupTable.addMouseListener(MatchUpListener4);
			} if (o == 4) {
				matchupButton.addMouseListener(MatchUpListener5);
				matchupTable.addMouseListener(MatchUpListener5);
			}
			
			//Adds the two labels to the button
			/*matchupButton.add(teamALabel);
			matchupButton.add(teamBLabel);*/
			
			matchupButton.add(matchupTable);
			matchups.add(matchupButton);
		} 
		
		//Create a new JPanel, -> set the background to be black (would like to have an "off-air" picture here instead somehow)
		JPanel liveVideo = new JPanel();
		liveVideo.setBackground(Color.black);
		//Set the correct bounds.
		liveVideo.setBounds(screenWidth - screenWidth/2, 0, screenWidth/2, screenHeight/2);
		liveVideo.setSize(screenWidth,screenHeight);
		liveVideo.setAlignmentX(Component.RIGHT_ALIGNMENT);
		liveVideo.setAlignmentY(Component.TOP_ALIGNMENT);
		
		MouseListener LVListener = new MouseAdapter() {
			  public void mouseClicked(MouseEvent event) {
			    newFrame.setVisible(false); // figure out the source
			    LVS.runLiveVideoScreen(league, matchUPS,recentScores); // display the text in the clicked label
			    // message is a JLabel
			  }
			};
			
		liveVideo.addMouseListener(LVListener);

		// Add all 4 of the Panels to the JFrame (newFrame)
		newFrame.getContentPane().add(topFive);
		newFrame.getContentPane().add(recentUpdates);
		newFrame.getContentPane().add(matchups);
		newFrame.getContentPane().add(liveVideo);

		// Pack it all up
		newFrame.pack();

		// Set the default close operation to eit when you click the X button
		newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
		// Center the JFrame
		newFrame.setLocationRelativeTo(null);
		
		// Set the window to full screen mode
		newFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		
		// Set the visibility
		newFrame.setVisible(true);
	}
	
//	private class recentListener implements MouseListener
//	{
//		public void mouseEntered(MouseEvent event)
//		{
//			newFrame.setVisible(false);
//			rSS.runRecentScoresScreen(recentScores);
//
//		} //end of actionPerformed method
//	} //end of ButtonListener class

}
